package com.tianbing.adlottery.controller.hordeadmin;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.fengniu.ejb.business.ICouponBusiness;
import com.fengniu.ejb.business.ISiteBusiness;
import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.ICategoryDao;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.dao.IUserDao;
import com.fengniu.ejb.entity.Category;
import com.fengniu.ejb.entity.Site;
import com.fengniu.ejb.entity.Site2Content;
import com.fengniu.ejb.entity.User;
import com.fengniu.ejb.entity.User2User;
import com.tianbing.adlottery.controller.user.AdminMySaleController;
import com.tianbing.adlottery.global.IConstants;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.UserSession;
import com.tianbing.adlottery.util.UtilDataGroup;

/**
 * MultiActionController for the image list/upload UI.
 * 
 * @author Conan Chen
 * @since 2006.12.15
 */
public class AdminServantController extends MultiActionController {

	Logger logger = Logger.getLogger(AdminMySaleController.class.getName());

	private String listMyServedStoreView;

	private String listMyServedStoreCouponView;

	private String myServedProductListView;

	private String myServedWebSiteListView;

	private String myRecommendServantListView;

	private String addMyRecommendServantSuccView;

	private String approveStoreServantSuccView;

	private String updateMyServedStoreCategoryView;

	private SkinUtils skinUtils;

	private IUserBusiness userBusiness;

	private ISiteBusiness siteBusiness;

	private ICouponBusiness couponBusiness;

	private ISiteDao siteDao;

	private IUserDao userDao;

	private ICategoryDao categoryDao;

	public ModelAndView listMyServedStore(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(listMyServedStoreView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);

		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = 30;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);

		Map u2sMap = userBusiness.getUserServedStores(auth.getUserSysId(), -1, pgno, pgsize);
		List user2SiteList = (List) u2sMap.get("user2SiteList");
		Long totalCount = (Long) u2sMap.get("totalCount");

		List user2SiteGroupList = UtilDataGroup.makeGroups(user2SiteList, 0, 4);

		model.put("user2SiteGroupList", user2SiteGroupList);

		model = skinUtils.preparePageFooter(request, user2SiteList, totalCount, pgno, pgsize, model);

		return mv;
	}

	@SuppressWarnings("unchecked")
	public ModelAndView listMyServedStoreCoupon(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ModelAndView mv = new ModelAndView(listMyServedStoreCouponView);
		Map<String, Object> model = mv.getModel();
		UserSession auth = skinUtils.getUserAuthorization(request, response);

		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = 30;// ServletRequestUtils.getIntParameter(request,
		// "pgsize", 30);

		Site siteDetail = siteDao.findSiteBySysId(ssid);

		Map<String, Object> s2cMap = couponBusiness.getCoupons(ssid, pgno, pgsize);

		List<Site2Content> site2ContentList = (List) s2cMap.get("site2ContentList");
		Long totalCount = (Long) s2cMap.get("totalCount");
		List site2ContentGroupList = UtilDataGroup.makeGroups(site2ContentList, 0, 4);

		model.put("siteDetail", siteDetail);
		model.put("site2ContentGroupList", site2ContentGroupList);

		model = skinUtils.preparePageFooter(request, site2ContentList, totalCount, pgno, pgsize, model);

		return mv;
	}

	@SuppressWarnings("unchecked")
	public ModelAndView listMyServedProduct(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(myServedProductListView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		// get site2content for this site
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");
		Map u2cMap = userBusiness.getUserServedProducts(auth.getUserSysId(), -1, pgno, pgsize);

		List user2ContentList = (List) u2cMap.get("user2ContentList");
		Long totalCount = (Long) u2cMap.get("totalCount");

		List user2ContentGroupList = UtilDataGroup.makeGroups(user2ContentList, 0, 6);

		model.put("user2ContentGroupList", user2ContentGroupList);
		model = skinUtils.preparePageFooter(request, user2ContentList, totalCount, pgno, pgsize, model);

		return mv;
	}

	@SuppressWarnings("unchecked")
	public ModelAndView listMyServedWebSite(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(myServedWebSiteListView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		// get site2content for this site
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);
		String odby = ServletRequestUtils.getStringParameter(request, "odby", "");

		Map u2sMap = userBusiness.getUserServedWebSites(auth.getUserSysId(), -1, pgno, pgsize);
		List user2SiteList = (List) u2sMap.get("user2SiteList");
		Long totalCount = (Long) u2sMap.get("totalCount");

		List user2SiteGroupList = UtilDataGroup.makeGroups(user2SiteList, 0, 6);

		model.put("user2SiteGroupList", user2SiteGroupList);
		model = skinUtils.preparePageFooter(request, user2SiteList, totalCount, pgno, pgsize, model);

		return mv;
	}

	public ModelAndView listMyRecommendServant(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ModelAndView mv = new ModelAndView(myRecommendServantListView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);

		User2User myRecommender = userBusiness.findUserRecommender(auth.getUserSysId());
		List<User2User> myRecommendeeList = userBusiness.getListUserRecommendee(auth.getUserSysId());
		User myself = userDao.findUserBySysId(auth.getUserSysId());
		model.put("myself", myself);
		model.put("myRecommender", myRecommender);
		model.put("myRecommendeeList", myRecommendeeList);
		model.put("myRecommendeeListSize", myRecommendeeList == null ? new Integer(0) : new Integer(myRecommendeeList
				.size()));

		// see this.addMyRecommendServant()
		model.put("userId", ServletRequestUtils.getStringParameter(request, "userId", null));
		model.put("hasRecommender", ServletRequestUtils.getStringParameter(request, "hasRecommender", null));
		return mv;
	}

	public ModelAndView addMyRecommendServant(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ModelAndView mv = new ModelAndView(addMyRecommendServantSuccView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);

		String userId = ServletRequestUtils.getStringParameter(request, "userId", null);
		if (userId != null) {
			User user = userDao.findUserByUserId(userId);
			if (user != null) {
				User2User recommender = userBusiness.findUserRecommender(user.getSysId());
				if (recommender != null) {
					model.put("hasRecommender", "Y");
				} else {
					User2User result = userBusiness.setUserRecommender(auth.getUserSysId(), user.getSysId());
				}
			}
		}
		model.put("userId", userId);
		return mv;
	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 * @see <code>AdminMyStoreController.requestMyStoreServant</code>
	 */
	public ModelAndView approve2BeStoreServant(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView(approveStoreServantSuccView);
		Map<String, Object> model = mv.getModel();
		// process wordtext parameter
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long u2ssid = ServletRequestUtils.getLongParameter(request, "u2ssid", -1);
		if (u2ssid > -1) {
			userBusiness.approve2BeStoreServant(auth.getUserSysId(), u2ssid);
		}

		return mv;
	}

	public ModelAndView updateMyServedStoreCategory(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView(updateMyServedStoreCategoryView);
		Map<String, Object> model = mv.getModel();

		String bizgid = ServletRequestUtils.getStringParameter(request, "bizgid", IConstants.CategoryID.BUSINESS_TOP
				.getId());
		if (bizgid != null && bizgid.trim() != "") {
			// get category by gid
			Category siteCategory = categoryDao.findCategoryByCategoryId(bizgid);
			List primarySiteCategoryTrail = categoryDao.getListCategoryPrimaryTrailBy(bizgid);
			// get child categories of gid
			List<Category> childCategoryList = categoryDao.getListChildCategoryBy(bizgid, 0, 100, null);
			List siteChildCategoryListGroups = UtilDataGroup.makeGroups(childCategoryList, 0, 8);
			model.put("primaryBusinessCategoryTrail", primarySiteCategoryTrail);
			model.put("businessChildCategoryListGroups", siteChildCategoryListGroups);
		}
		String areagid = ServletRequestUtils.getStringParameter(request, "areagid", IConstants.CategoryID.AREA_TOP
				.getId());
		if (areagid != null && areagid.trim() != "") {
			// get category by gid
			Category siteCategory = categoryDao.findCategoryByCategoryId(areagid);
			List primarySiteCategoryTrail = categoryDao.getListCategoryPrimaryTrailBy(areagid);
			// get child categories of gid
			List<Category> childCategoryList = categoryDao.getListChildCategoryBy(areagid, 0, 100, null);
			List siteChildCategoryListGroups = UtilDataGroup.makeGroups(childCategoryList, 0, 8);
			model.put("primaryAreaCategoryTrail", primarySiteCategoryTrail);
			model.put("areaChildCategoryListGroups", siteChildCategoryListGroups);
		}

		long ssid = ServletRequestUtils.getLongParameter(request, "ssid", -1);
		if (ssid > 0) {
			Site siteDetail = siteDao.findSiteBySysId(ssid);
			model.put("siteDetail", siteDetail);

			List categoryTrails = siteBusiness.getSiteCategoryTrails(ssid, IConstants.CategoryID.AREA_TOP.getId());
			model.put("storeAreaCategoryTrails", categoryTrails);

			List categoryBizTrails = siteBusiness.getSiteCategoryTrails(ssid, IConstants.CategoryID.BUSINESS_TOP
					.getId());
			model.put("storeBusinessCategoryTrails", categoryBizTrails);

			// -------------add/delete site as/from category
			// membership-----------------
			String sgid = ServletRequestUtils.getStringParameter(request, "sgid", null);
			String method = ServletRequestUtils.getStringParameter(request, "method", null);
			if (method != null) {
				if (method.equals("add")) {
					siteBusiness.addSiteAsCategoryMember(ssid, sgid);
				} else if (method.equals("del")) {
					siteBusiness.deleteSiteFromCategoryMember(ssid, sgid);
				}
			}

		}
		return mv;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public void setListMyServedStoreView(String listMyServedStoreView) {
		this.listMyServedStoreView = listMyServedStoreView;
	}

	public void setListMyServedStoreCouponView(String listMyServedStoreCouponView) {
		this.listMyServedStoreCouponView = listMyServedStoreCouponView;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	public void setSiteBusiness(ISiteBusiness siteBusiness) {
		this.siteBusiness = siteBusiness;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setMyServedProductListView(String myServedProductListView) {
		this.myServedProductListView = myServedProductListView;
	}

	public void setMyServedWebSiteListView(String myServedWebSiteListView) {
		this.myServedWebSiteListView = myServedWebSiteListView;
	}

	public void setMyRecommendServantListView(String myRecommendServantListView) {
		this.myRecommendServantListView = myRecommendServantListView;
	}

	public void setAddMyRecommendServantSuccView(String addMyRecommendServantSuccView) {
		this.addMyRecommendServantSuccView = addMyRecommendServantSuccView;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setApproveStoreServantSuccView(String approveStoreServantSuccView) {
		this.approveStoreServantSuccView = approveStoreServantSuccView;
	}

	public void setCouponBusiness(ICouponBusiness couponBusiness) {
		this.couponBusiness = couponBusiness;
	}

	public void setUpdateMyServedStoreCategoryView(String updateMyServedStoreCategoryView) {
		this.updateMyServedStoreCategoryView = updateMyServedStoreCategoryView;
	}

	public void setCategoryDao(ICategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

}
